package model

import (
	"github.com/jinzhu/gorm"
	"time"
	"github.com/satori/go.uuid"
)

type AccessToken struct {
	gorm.Model
	ClientID   uint `sql:"index;not null"`
	AccountId   uint `sql:"index"`
	Client    *Client
	Account      *Account
	Token     string    `sql:"type:varchar(40);unique;not null"`
	ExpiresAt time.Time `sql:"not null;default:now()"`
	Scope     string    `sql:"type:varchar(200);not null"`
}


// NewAccessToken creates new AccessToken instance
func NewAccessToken(client *Client, account *Account, expiresIn int64, scope string) *AccessToken {
	u,_:=uuid.NewV4()

	accessToken := &AccessToken{
		ClientID:  client.ID,
		Token:u.String(),
		ExpiresAt: time.Now().UTC().Add(time.Duration(expiresIn) * time.Second),
		Scope:     scope,
	}
	if account != nil {
		accessToken.AccountId = account.ID
	}
	return accessToken
}
